<?php
	/**
	*用户管理类
	**/
	class AdminClass {
		// 构造函数
		public function __constract(){
		}

		// 获取用户列表
		public function getList($data){
			global $db;
			$type = $data->type;

			if($type == 1){
				// 所有用户信息
				$db->query("select SQL_CALC_FOUND_ROWS
								user_list.id,
								user_list.user_name as name,
								user_list.type,
								user_list.email,
								user.name as creator_name,
								user_list.last_login_time,
								user_list.role_name,
								user_list.role_id,
								user_list.flag
							from user_list 
								left join admin_user as user
								on user_list.creator=user.id 
								order by user_list.id asc", "");
			} else {
				$pageCurrent = $data->pageCurrent;
				$pageSize = $data->pageSize;
				// 按分页获取用户信息
				$db->query("select SQL_CALC_FOUND_ROWS
								user_list.id,
								user_list.user_name as name,
								user_list.type,
								user_list.email,
								user.name as creator_name,
								user_list.last_login_time,
								user_list.role_name,
								user_list.role_id,
								user_list.flag
							from user_list 
								left join admin_user as user
								on user_list.creator=user.id 
								order by user_list.id asc limit :start,:count ", 
							array(
								"start"=>($pageCurrent-1) * $pageSize,
								"count"=> $pageSize
								)
							);
			}
			
			$userList = $db->prepare->fetchAll();

			// 获取总条数
			$db->query("select FOUND_ROWS() as total", "");
			$total = $db->prepare->fetchAll();
		
			$data = array(
					"total"=>$total[0]['total'],
					"data"=>$userList
				);

			return $data;
		}

		// 添加用户
		public function add($data){
			global $db, $session;
			$loginUserId = $session->get("userId");
			$name = $data->name;
			$password = $data->password;
			$email = $data->email;
			$type = $data->type;
			$roleId = $data->roleId;

			// 更新admin_login表
			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 检查用户名称是否已经存在
			$db->query("select *
						from admin_user 
						where 
							name=:name
						for update",
						array(
							"name"=>$name
							)
						);
			$result = $db->prepare->fetchAll();
			if($result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "用户名称已经存在了，请重新命名！", null);
			}

			// 添加一条用户信息
			$db->query("insert into admin_user (
							name,
							password,
							email,
							type,
							creator) 
						values(
							:name,
							:password,
							:email,
							:type,
							:creator)",
						array(
							"name"=>$name,
							"password"=>$password,
							"email"=>$email,
							"type"=>$type,
							"creator"=>$loginUserId)
						);
			$result = $db->prepare->rowCount();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "操作失败！", null);
			}

			// 查找新添加的一条数据的id
			$db->query("select last_insert_id() as id", "");
			$result = $db->prepare->fetchAll();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "操作失败！", null);
			}
			$userId = $result[0]["id"];

			// 添加admin_user_role数据
			$db->query("insert into admin_user_role (
							userid,
							roleid,
							creator
						) 
						values(
							:userid,
							:roleid,
							:creator)",
						array(
							"userid"=>$userId,
							"roleid"=>$roleId,
							"creator"=>$loginUserId
							)
						);
			$result = $db->prepare->rowCount();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "操作失败！", null);
			}
			$db->PDO->commit();
			// 事物结束
		}

		// 删除用户
		public function del($data){
			global $db;
			$userId = $data->id;
			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 删除admin_user_role中数据
			$db->query("delete from admin_user_role 
						where 
							userid=:id",
						array(
							"id"=>$userId
							)
						);
			$result = $db->prepare->rowCount();

			// 删除admin_user表中的数据
			$db->query("delete from admin_user
						where
							id=:id
						", 
						array(
						"id"=>$userId
							)
						);

			$db->PDO->commit();
			// 事物结束
		}

		// 启用、禁用用户
		public function enable($data){
			global $db;
			$userId = $data->id;
			$flag = $data->flag;

			$db->query("update admin_user 
						set 
							flag=:flag
						where
							id=:id
						",
						array(
							"flag"=>$flag,
							"id"=>$userId
							)
						);
			$result = $db->prepare->rowCount();
			if(!$result){
				returnAjaxJson(400, "操作失败！", null);
			}
		}

		// 修改用户密码
		public function pwd($data){
			global $db,$session;
			$loginUserId = $session->get("userId");
			$oldPassword = $data->oldPassword;
			$newPassword = $data->newPassword;

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 判断原密码是否正确
			$db->query("select
							id
						from admin_user
						where
							id=:id and
							password=:password
						for update",
						array(
							"password"=>$oldPassword,
							"id"=>$loginUserId
							)
						);
			$result = $db->prepare->fetchAll();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "原密码错误！", null);
			}

			// 更新新密码
			$db->query("update admin_user 
						set 
							password=:password
						where
							id=:id
						",
						array(
							"password"=>$newPassword,
							"id"=>$loginUserId
							)
						);
			$result = $db->prepare->rowCount();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "操作失败！", null);
			}

			$db->PDO->commit();
			// 事物结束
		}

		// 编辑用户信息
		public function edit($data){
			global $db;
			$userId = $data->id;
			$name = $data->name;
			$password = $data->password;
			$email = $data->email;
			$type = $data->type;
			$roleId = $data->roleId;

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 检查用户名称是否已经存在
			$db->query("select *
						from admin_user 
						where 
							name=:name and
							id!=:id
						for update",
						array(
							"name"=>$name,
							"id"=>$userId
							)
						);
			$result = $db->prepare->fetchAll();
			if($result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "用户名称已经存在了，请重新命名！", null);
			}

			// 更新admin_user数据
			$db->query("update admin_user 
						set 
							name=:name,
							password=:password,
							email=:email,
							type=:type
						where
							id=:id
						",
						array(
							"name"=>$name,
							"password"=>$password,
							"email"=>$email,
							"type"=>$type,
							"id"=>$userId
							)
						);
			$result = $db->prepare->rowCount();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "操作失败！", null);
			}

			// 更新admin_user_role数据
			$db->query("update admin_user_role 
						set 
							roleid=:roleid
						where
							userid=:id
						",
						array(
							"roleid"=>$roleId,
							"id"=>$userId
							)
						);

			$db->PDO->commit();
			// 事物结束
		}

		// 获取用户可操作菜单列表
		public function getMenu(){
			global $db,$session;
			$loginUserId = $session->get("userId");
			$loginUserType = $session->get("userType");

			// 登录用户是否是超级管理员或者是普通管理员
			if($loginUserType == 0){
				$db->query("select
								menu.id,
								menu.name,
								menu.parent_id,
								menu.url,
								menu.type
							from admin_menu as menu 
							where 
								flag=1
							order by menu.listorder asc,menu.id asc", "");
			} else{
				$db->query("select
								menu.id,
								menu.name,
								menu.parent_id,
								menu.url,
								menu.type
							from admin_user as user
							right join admin_user_role as user_role
							on user.id=user_role.userid
							right join admin_role_menu as role_menu
							on user_role.roleid=role_menu.roleid
							left join admin_menu as menu
							on role_menu.menuid=menu.id
							where 
								user.id=:id and
								menu.flag=1
							order by menu.listorder asc,menu.id asc",
						array(
							"id"=>$loginUserId
							)
						);
			}
			return $db->prepare->fetchAll();
		}
		
	}
?>